package com.ruoyi.web.controller.gwc;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gwc.domain.TFuelCardManagement;
import com.ruoyi.gwc.service.ITFuelCardManagementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 加油卡管理Controller
 * 
 * @author wp
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/gwc/carManagement")
public class TFuelCardManagementController extends BaseController
{
    @Autowired
    private ITFuelCardManagementService tFuelCardManagementService;

    /**
     * 查询加油卡管理列表
     */
    @ApiOperation("查询加油卡管理列表")
    @GetMapping("/list")
    public TableDataInfo list(TFuelCardManagement tFuelCardManagement)
    {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((tFuelCardManagement.getCompanyId()))) {
            tFuelCardManagement.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<TFuelCardManagement> list = tFuelCardManagementService.selectTFuelCardManagementList(tFuelCardManagement);
        for (TFuelCardManagement fuelCardManagement : list ){
            if(ObjectUtils.isNotEmpty(fuelCardManagement.getAmount())) {
                fuelCardManagement.setAmountStr(fuelCardManagement.getAmount().toString());
            }
            if(ObjectUtils.isNotEmpty(fuelCardManagement.getMileage())) {
                fuelCardManagement.setMileageStr(fuelCardManagement.getMileage().toString());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出加油卡管理列表
     */
    @Log(title = "加油卡管理", businessType = BusinessType.EXPORT)
    @ApiOperation("导出加油卡管理列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TFuelCardManagement tFuelCardManagement)
    {
        List<TFuelCardManagement> list = tFuelCardManagementService.selectTFuelCardManagementList(tFuelCardManagement);
        ExcelUtil<TFuelCardManagement> util = new ExcelUtil<TFuelCardManagement>(TFuelCardManagement.class);
        util.hideColumn("remark");
        util.exportExcel(response, list, "加油卡管理数据");
    }

    /**
     * 获取加油卡管理详细信息
     */
    @ApiOperation("获取加油卡管理详细信息")
    @GetMapping(value = "/{fuelId}")
    public AjaxResult getInfo(@PathVariable("fuelId") Long fuelId)
    {
        return success(tFuelCardManagementService.selectTFuelCardManagementByFuelId(fuelId));
    }

    /**
     * 新增加油卡管理
     */
    @ApiOperation("新增加油卡管理")
    @Log(title = "加油卡管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TFuelCardManagement tFuelCardManagement)
    {
        tFuelCardManagement.setAmount(new BigDecimal(tFuelCardManagement.getAmountStr()));
        tFuelCardManagement.setMileage(new BigDecimal(tFuelCardManagement.getMileageStr()));
        return toAjax(tFuelCardManagementService.insertTFuelCardManagement(tFuelCardManagement));
    }

    /**
     * 修改加油卡管理
     */
    @ApiOperation("修改加油卡管理")
    @Log(title = "加油卡管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TFuelCardManagement tFuelCardManagement)
    {
        tFuelCardManagement.setAmount(new BigDecimal(tFuelCardManagement.getAmountStr()));
        tFuelCardManagement.setMileage(new BigDecimal(tFuelCardManagement.getMileageStr()));
        return toAjax(tFuelCardManagementService.updateTFuelCardManagement(tFuelCardManagement));
    }

    /**
     * 删除加油卡管理
     */
    @ApiOperation("删除加油卡管理")
    @Log(title = "加油卡管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fuelIds}")
    public AjaxResult remove(@PathVariable Long[] fuelIds)
    {
        return toAjax(tFuelCardManagementService.deleteTFuelCardManagementByFuelIds(fuelIds));
    }
}
